<?php

require_once 'model/unit.php';
require_once 'model/army.php';
require_once 'model/magic.php';
require_once 'model/hero.php';
require_once 'lib/simulator.php';
require_once 'config/unitlist.php';
require_once 'config/magiclist.php';
require_once 'lib/tinyurl.php';

if (isset($_GET['token_v1']))
{
    $token_data = json_decode(gzuncompress(base64_decode(rawurldecode($_GET['token_v1']))), true);
    $_POST = $token_data;
    $_REQUEST = $token_data;
}
$token = rawurlencode(base64_encode(gzcompress(json_encode($_POST), 9)));


$result = array();
$attacker = new hero();
$attacker->race = @$_REQUEST["attacker_race"];
$attacker->defatt_level = isset($_REQUEST["attacker_attack_level"]) ? $_REQUEST["attacker_attack_level"] : 0;
$attacker->harrang_goodpay_level = isset($_REQUEST["attacker_harangue_level"]) ? $_REQUEST["attacker_harangue_level"] : 0;
$attacker->tactician_level = isset($_REQUEST["attacker_tactician_level"]) ? $_REQUEST["attacker_tactician_level"] : 0;
$attacker->cavalry_level = isset($_REQUEST["attacker_rider_level"]) ? $_REQUEST["attacker_rider_level"] : 0;
$attacker->archer_level = isset($_REQUEST["attacker_marksman_level"]) ? $_REQUEST["attacker_marksman_level"] : 0;
$attacker->infantry_level = isset($_REQUEST["attacker_infantry_level"]) ? $_REQUEST["attacker_infantry_level"] : 0;
$attacker->magic_level = @$_REQUEST["attacker_magic_level"];
$defender = new hero();
$defender->race = @$_REQUEST["defender_race"];
$defender->defatt_level = isset($_REQUEST["defender_defence_level"]) ? $_REQUEST["defender_defence_level"] : 0;
$defender->harrang_goodpay_level = isset($_REQUEST["defender_goodpay_level"]) ? $_REQUEST["defender_goodpay_level"] : 0;
$defender->tactician_level = isset($_REQUEST["defender_tactician_level"]) ? $_REQUEST["defender_tactician_level"] : 0;
$defender->cavalry_level = isset($_REQUEST["defender_rider_level"]) ? $_REQUEST["defender_rider_level"] : 0;
$defender->archer_level = isset($_REQUEST["defender_marksman_level"]) ? $_REQUEST["defender_marksman_level"] : 0;
$defender->infantry_level = isset($_REQUEST["defender_infantry_level"]) ? $_REQUEST["defender_infantry_level"] : 0;
$defender->magic_level = @$_REQUEST["defender_magic_level"];
$sim = new simulator($attacker, $defender);

for($i = 0; $i < 7; $i++)
{
    if (@$_REQUEST["attacker_class_$i"] > 0 && @$_REQUEST["attacker_quantity_$i"])
    {
        $u = $_UNIT_LIST_SORTED[$_REQUEST["attacker_class_$i"]];
        $sim->attacker->army->add_unit(new unit($u[1], $u[2], $u[3], $_REQUEST["attacker_quantity_$i"], $u[0]));
    }
    else if (@$_REQUEST["attacker_type_$i"] && @$_REQUEST["attacker_quantity_$i"] && @$_REQUEST["attacker_power_$i"])
    {
        $sim->attacker->army->add_unit(new unit($_REQUEST["attacker_power_$i"], $_REQUEST["attacker_type_$i"], 1,
                $_REQUEST["attacker_quantity_$i"], $_REQUEST["attacker_name_$i"] ? $_REQUEST["attacker_name_$i"] : NULL));
    }
    if (@$_REQUEST["defender_class_$i"] > 0 && @$_REQUEST["defender_quantity_$i"])
    {
        $u = $_UNIT_LIST_SORTED[$_REQUEST["defender_class_$i"]];
        $sim->defender->army->add_unit(new unit($u[1], $u[2], $u[3], $_REQUEST["defender_quantity_$i"], $u[0]));
    }
    else if (@$_REQUEST["defender_type_$i"] && @$_REQUEST["defender_quantity_$i"] && @$_REQUEST["defender_power_$i"])
    {
        $sim->defender->army->add_unit(new unit($_REQUEST["defender_power_$i"], $_REQUEST["defender_type_$i"], 1,
                $_REQUEST["defender_quantity_$i"], $_REQUEST["defender_name_$i"] ? $_REQUEST["defender_name_$i"] : NULL));
    }
}

if (@$_REQUEST["attacker_magic_1"] && @$_REQUEST["attacker_magic_turn_1"])
{
    $sim->attacker->add_magic(@$_REQUEST["attacker_magic_turn_1"], $_MAGIC_LIST[@$_REQUEST["attacker_magic_1"]]);
}
if (@$_REQUEST["attacker_magic_2"] && @$_REQUEST["attacker_magic_turn_2"])
{
    $sim->attacker->add_magic(@$_REQUEST["attacker_magic_turn_2"], $_MAGIC_LIST[@$_REQUEST["attacker_magic_2"]]);
}
if (@$_REQUEST["defender_magic_1"] && @$_REQUEST["defender_magic_turn_1"])
{
    $sim->defender->add_magic(@$_REQUEST["defender_magic_turn_1"], $_MAGIC_LIST[@$_REQUEST["defender_magic_1"]]);
}
if (@$_REQUEST["defender_magic_2"] && @$_REQUEST["defender_magic_turn_2"])
{
    $sim->defender->add_magic(@$_REQUEST["defender_magic_turn_2"], $_MAGIC_LIST[@$_REQUEST["defender_magic_2"]]);
}


while (($turn = $sim->run()))
{
    $result[] = $turn;
}

foreach ($_UNIT_LIST_SORTED as $k => $v)
{
    $units_display[$k] = $v[0];
    if ($k != 0)
    $units_display[$k] .= ' (' . unit::type_to_name($v[2]) . ') ' . $v[1];
}

foreach ($_MAGIC_LIST as $k => $v)
{
    $magics_display[$k] = $v[0];
    if ($k != 0)
    $magics_display[$k] = $v[0] . ' : ' . $v[2] . ' (' . $v[3] . ')';
}


$winner = $sim->get_winner();
$att_base_xp = $sim->attacker->get_base_xp($sim->defender);
$att_raw_power = $sim->attacker->get_raw_power();
$def_raw_power = $sim->defender->get_raw_power();

?>
